package longrise.phone.com.bjjt_henan.widget;

import android.Manifest;
import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.longrise.android.FrameworkManager;

import java.util.List;

import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.CommontUtils;
import longrise.phone.com.bjjt_henan.Utils.PermissionHelper;
import longrise.phone.com.bjjt_henan.widget.photo.PhotoSelectView;

/**
 * Created by Administrator on 2016/5/19.
 */
public class PictureSelectDialog extends Dialog {

    private Context mContext;

    private Button takephotoBtn;

    private Button albumBtn;

    private Button cancelBtn;

    private onActionListener listener;

    public PictureSelectDialog(Context context) {
        super(context, R.style.AlertDialogStyle);
        mContext = context;

        View view = LayoutInflater.from(context).inflate(R.layout.change_headportrait, null);
        initViews(view);
        setContentView(view);
        //设置大小
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.80);
        dialogWindow.setAttributes(lp);
        setEvent();
    }

    private void initViews(View view) {
        takephotoBtn = (Button) view.findViewById(R.id.changetakephoto);
        albumBtn = (Button) view.findViewById(R.id.changefrom_album);
        cancelBtn = (Button) view.findViewById(R.id.changenagative);
    }

    private void setEvent() {
        takephotoBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onCamera();
                }
            }
        });
        albumBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!PermissionHelper.checkPermissions(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    return;
                }
                dismiss();
                PhotoSelectView view = new PhotoSelectView(mContext);
                view.setMode(1);
                view.setOnSelectImgFinishListener(new PhotoSelectView.OnSelectImgFinishListener() {
                    @Override
                    public void onSelectFinish(PhotoSelectView photoView, List<String> list) {
                        String path = CommontUtils.bitmapNewWaH(list.get(0), mContext);
                        if (null != listener) {
                            listener.onAlbum(path);
                        }
                    }
                });
                FrameworkManager.getInstance().showNewForm(mContext, view);
            }
        });
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
    }

    public void setOnSelectListener(onActionListener listener) {
        this.listener = listener;
    }

    public interface onActionListener {

        void onCamera();//拍照

        void onAlbum(String path);//相册
    }
}
